// pages/mine/store/store.js
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk');
const validate = require('../../../../utils/validate')
var { _save_store } = require('../../../../utils/xiaohuangyu')
var qqmapsdk;
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    store: {
      city_name: '',
      name: '',
      describe: '',
      thumbnail: -1,
      address: '',
      address_detail: '',
      latitude: 0,
      longitude: 0,
    },
    imgList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    qqmapsdk = new QQMapWX({
      key: app.globalData.qqmapsdk_key
    });
  },
  saveStoreInfoTap() {
    var store_data = this.data.store
    if (validate.isEmpty(store_data['name']) || store_data['name'].length > 20) {
      validate.showToast('请输入1-25长度的店铺名称')
      return
    }
    if (store_data['thumbnail'] < 0) {
      validate.showToast('请上传商铺logo')
      return
    }
    if (validate.isEmpty(store_data['address'])) {
      validate.showToast('地址不能为空')
      return
    }
    if (validate.isEmpty(store_data['address_detail'])) {
      validate.showToast('详细地址不能为空')
      return
    }
    _save_store(store_data, res => {
      if (res.status) {
        wx.showToast({
          title: '保存成功!',
        })
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  },
  addressDetailInputBlur(res) {
    this.setData({
      [`store.address_detail`]: res.detail.value,
    })
  },
  describeInputBlur(res) {
    this.setData({
      [`store.describe`]: res.detail.value,
    })
  },
  nameInputBlur(res) {
    this.setData({
      [`store.name`]: res.detail.value,
    })
  },
  // 打开地图选择位置
  chooseLocation() {
    const that = this
    wx.chooseLocation({
      success(res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (reqs) {//成功后的回调
            that.setData({
              [`store.address`]: res.address,
              [`store.address_detail`]: res.name,
              [`store.longitude`]: res.longitude,
              [`store.latitude`]: res.latitude,
              [`store.city_name`]: reqs.result.address_component.city,
            })
          }
        })
      }
    })
  },
  // 图片
  ChooseImage() {
    const that = this
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        const tempFilePaths = res.tempFilePaths
        app.uploadFile(tempFilePaths[0], 'store', reqs => {
          const result = JSON.parse(reqs)
          // console.log(result)
          that.setData({
            imgList: [tempFilePaths[0]],
            [`store.thumbnail`]: result.data.id,
          })
        })
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    this.data.imgList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: this.data.imgList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})